package com.chezhujie.www.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.chezhujie.www.constant.PageInfo;
import com.chezhujie.www.core.page.Pagination;
import com.chezhujie.www.domain.Czjdataresource;
import com.chezhujie.www.persistence.DataresourceMapper;

/**
 * Created by 汤伟杰 on 2017/5/18.
 */
@Service
public class SortingService {
    private static final String code = "SORT.CXSX";
    
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    @Autowired
    private DataresourceService dataresourceService;
    
    @Autowired
    private DataresourceMapper dataresourceMapper;

    public void exchange(String id, Integer sorting) throws Exception {
        Integer location=null;
        Integer no = null;
        Czjdataresource czjdataresource = (Czjdataresource) jdbcTemplate.queryForObject("select * from czj_xy_dataresource where dataresourceId='" + id + "' order by createtime desc limit 1", new BeanPropertyRowMapper(Czjdataresource.class));
        try {
            //位置顺序
            no = Integer.parseInt(String.valueOf(czjdataresource.getValue()));
            location = no+sorting;
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("切换顺序失败！顺序值只能是数字，但出现 "+czjdataresource.getValue());
        }
        //查询已存在的进行修改
        String update1 = "update czj_xy_dataresource set value='"+no+"' where code='" + code + "' and value='" + location + "'";
        jdbcTemplate.update(update1);
        //更新id交换的数据
        String update2 = "update czj_xy_dataresource set value='"+location+"' where dataresourceId='"+id+"'";
        jdbcTemplate.update(update2);

    }

    public Pagination list() {
        Pagination pagination = new Pagination();
        pagination.setPageNo(1);
        pagination.setPageSize(1000);
        List<Map<String, Object>> listmap = jdbcTemplate.queryForList("select * from czj_xy_dataresource where code='" + code + "' order by value asc");
        pagination.setList(listmap);
        pagination.setTotalCount(listmap.size());
        return pagination;
    }
    
    public List<Map<String, Object>> listApi() throws Exception {
       
        List<Map<String, Object>> listmap = jdbcTemplate.queryForList("select * from czj_xy_dataresource where code='" + code + "' order by value asc");
  
        return listmap;
    }

	public Pagination listHyjg() {
        Pagination pagination = new Pagination();
        pagination.setPageNo(1);
        pagination.setPageSize(1000);
        List<Map<String, Object>> listmap = jdbcTemplate.queryForList("select * from czj_xy_dataresource where code='" + PageInfo.hyjg + "'");
        pagination.setList(listmap);
        pagination.setTotalCount(listmap.size());
        return pagination;
	}

	public Czjdataresource getCzjdataresource(String dataresourceId) throws Exception{
		
	
		
		
		return dataresourceMapper.getCzjdataresource(dataresourceId);
	
	        
	}

	public void updateHyjg(java.math.BigDecimal value,String id) {
		jdbcTemplate.update("update czj_xy_dataresource set value='"+value+"' where dataresourceId='"+id+"'");
	}

	public Czjdataresource getCzjdataresourceByCode() {
		return dataresourceMapper.getCzjdataresourceByCode();
	}
}
